package edu.pe.cayetano.view.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.RowSetDynaClass;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ws.synopsis.frmwrk.common.BResult;
import edu.pe.cayetano.model.bean.PacienteBean;
import edu.pe.cayetano.model.bo.PacienteBO;
import edu.pe.cayetano.seccd.constantes.ConstantesSECCD;
import edu.pe.cayetano.seccd.constantes.ConstantesSession;
import edu.pe.cayetano.util.EscribeLog;
import edu.pe.cayetano.util.Utilitario;
import edu.pe.cayetano.util.UtilitarioSession;

public class PacienteAction extends BaseAction{
	
	private String page	= ConstantesSECCD.EXITO;
	
	public ActionForward listar(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
		BResult				ret			= new BResult();
		PacienteBO			pacienteBO 	= new PacienteBO();
		try {
			HttpSession session = request.getSession(false);
			ret = pacienteBO.listarPaciente();
			if(ret.getCode()==0 && ret.getObject()!=null){
				page=ConstantesSECCD.EXITO;
				UtilitarioSession.eliminaParametrosBusquedaGenerico(session);
				UtilitarioSession.seteaParametrosListadoGenerico(session,(RowSetDynaClass)ret.getObject());
			}else{
				page=ConstantesSECCD.ERROR;
				EscribeLog.escribeLog("PacienteAction","listar",ConstantesSECCD.ERROR,ret.getMessage());
				mostrarMensaje(request,ConstantesSECCD.ERROR_PACIENTE_LISTAR,new String[0],"home.do","home");
			}
		}catch (Exception e) {
			page=ConstantesSECCD.ERROR;
			EscribeLog.escribeLog("PacienteAction","listar",ConstantesSECCD.ERROR,ret.getMessage());
			mostrarMensaje(request,ConstantesSECCD.ERROR_PACIENTE_LISTAR,new String[0],"home.do","home");
		}
		return mapping.findForward(page);	
	}
		
	public ActionForward buscaPaciente(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
		BResult				ret			= new BResult();
		PacienteBO			pacienteBO 	= new PacienteBO();
		try {
			HttpSession 		session 	= request.getSession(false);
			DynaBean			dyna		= Utilitario.createDynaBeanFromRequest("dynaBean", request);
			ret = pacienteBO.buscarPaciente(dyna);
			if(ret.getCode()==0 && ret.getObject()!=null){
				page=ConstantesSECCD.EXITO;
				UtilitarioSession.seteaParametrosBusquedaGenerico(session,(RowSetDynaClass)ret.getObject(),dyna);
			}else{
				page=ConstantesSECCD.ERROR;
				EscribeLog.escribeLog("PacienteAction","buscaPaciente",ConstantesSECCD.ERROR,ret.getMessage());
				mostrarMensaje(request,ConstantesSECCD.ERROR_PACIENTE_BUSQUEDA,new String[0],"paciente.do","listar");
			}
		}catch (Exception e) {
			page=ConstantesSECCD.ERROR;
			EscribeLog.escribeLog("PacienteAction","buscaPaciente",ConstantesSECCD.ERROR,ret.getMessage());
			mostrarMensaje(request,ConstantesSECCD.ERROR_PACIENTE_BUSQUEDA,new String[0],"paciente.do","listar");
		}
		return mapping.findForward(page);	
	}

	public ActionForward verPaciente(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response){
		BResult					ret			= new BResult();
		PacienteBO				pacienteBO 	= null;
		try {
			HttpSession session = request.getSession(false);
			pacienteBO 	= new PacienteBO();
			DynaBean			dyna		= Utilitario.createDynaBeanFromRequest("dynaBean", request);
			ret = pacienteBO.buscarPacienteEspecifico(dyna);
			if(ret.getCode()==0 && ret.getObject()!=null){
				page=ConstantesSECCD.EXITO;
				session.setAttribute( ConstantesSession.PACIENTETO,(PacienteBean)ret.getObject());
				request.setAttribute(ConstantesSession.PACIENTETO, (PacienteBean)ret.getObject());
			}else{
				page=ConstantesSECCD.ERROR;
				EscribeLog.escribeLog("PacienteAction","verPaciente",ConstantesSECCD.ERROR,ret.getMessage());
				mostrarMensaje(request,ConstantesSECCD.ERROR_PACIENTE_VER,new String[0],"paciente.do","listar");
			}
		}catch (Exception e) {
			page=ConstantesSECCD.ERROR;
			EscribeLog.escribeLog("PacienteAction","verPaciente",ConstantesSECCD.ERROR,ret.getMessage());
			mostrarMensaje(request,ConstantesSECCD.ERROR_PACIENTE_VER,new String[0],"paciente.do","listar");
		}
		return mapping.findForward(page);	
	}	
}
